<template>
  <!-- 打分页面 -->
  <div class="app-container">
    <el-descriptions title="辅导员信息" :column="4" size="medium "	>
    <el-descriptions-item label="批次名称">{{queryParams.batchName}}</el-descriptions-item>
    <el-descriptions-item label="辅导员姓名">{{queryParams.clrName}}</el-descriptions-item>
    <el-descriptions-item label="辅导员教工号">{{queryParams.clrTno}}</el-descriptions-item>
    <el-descriptions-item label="所在院系">{{queryParams.deptName}}</el-descriptions-item>
</el-descriptions>
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" label-width="68px">
      <el-form-item label="批次名称">
        <el-input v-model="queryParams.batchName" />
      </el-form-item>
      <el-form-item label="辅导员姓名">
        <el-input v-model="queryParams.clrName" />
      </el-form-item>
      <el-form-item label="辅导员教工号">
        <el-input v-model="queryParams.clrTno" />
      </el-form-item>
      <el-form-item label="所在院系">
        <el-input v-model="queryParams.deptName" />
      </el-form-item>
    </el-form> -->
    <el-table
      :data="queryParams.fields"
      border
      :span-method="objectSpanMethod"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />-->
      <el-table-column label="指标分类名称" align="center" prop="classifyName" />
      <el-table-column label="指标名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="考核要点" align="center" prop="econtent">
        <!-- <template slot-scope="{row}">
          <el-popover trigger="hover">
            <div  v-html="row.econtent">
            </div>
          <div slot="reference">
            <span v-html="row.econtent" size="medium"></span>
          </div>
        </el-popover>
        </template> -->
      </el-table-column>
      <el-table-column label="赋分办法" align="center" prop="scoringWay">
        <!-- <template slot-scope="{ row }">
          <el-popover trigger="hover"  placement="top">
            <div  class="hoverText">
              <span  v-html="row.scoringWay" >
            </span>
            </div>
            
          <div slot="reference">
            <span v-html="row.scoringWay" size="medium"></span>
          </div>
        </el-popover>
        </template> -->
      </el-table-column>
      <el-table-column label="打分" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-input v-model="scope.row.scores" />
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-button>确定</el-button> -->
    <el-button type="primary" @click="submit" style="width:20%;display:block;margin:10px auto">确定</el-button>
  </div>
</template>

<script>
import { scoring } from "@/api/clr/examinemonad";
export default {
  name: "Examinemonad",
  dicts: ["clr_exam_systype"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 辅导员考核人员表格数据
      examinemonadList: [],
      indexArray: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cebId: null,
        cId: null,
        clrTno: null,
        clrName: null
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    
  },
  watch: {
    $route (to, from) {
      console.log(to, from)
      // this.渲染数据的函数()
      this.queryParams = this.$route.params
    }
  },
  mounted() {
    this.queryParams = this.$route.params;
    console.log(this.$route.params.fields, "this.$route.params.id");
    let count = 0;
    for (let rowIndex = 0; rowIndex < this.queryParams.fields.length; ) {
      this.indexArray.push(rowIndex);
      count = this.getRows(
        rowIndex,
        this.queryParams.fields[rowIndex].classifyName
      );
      rowIndex += count;
    }
  },
  methods: {
    submit() {
      console.log(
        this.queryParams.fields,
        this.queryParams.fields.some(val => !val.scores)
      );
      if (
        this.queryParams.fields.some(val => !val.scores) ||
        this.queryParams.fields.some(val => val.scores == "") ||
        this.queryParams.fields.some(val => val.score < val.scores) ||
        this.queryParams.fields.some(val => val.scores <= "0")
      ) {
        this.$modal.msgWarning(
          "打分的分数不能超过指标项的最高分且不能为空、不能为0 分"
        );
        return;
      } else {
        let arr = this.queryParams.fields.map(item => {
          return {
            cefId: item.cefId,
            score: item.scores,
            cemId: this.queryParams.cemId,
            ceeId: this.queryParams.ceeId
          };
        });
        scoring(arr).then(res => {
          console.log(res);
          if (res.code == 200) {
            this.$modal.msgSuccess("打分成功");
            this.$router.push("/counselor/exam/examinemonad");
          }
        });
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cemId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        let rowCount = 0;
        if (this.indexArray.includes(rowIndex)) {
          rowCount = this.getRows(rowIndex, row.classifyName);
          return {
            rowspan: rowCount,
            colspan: 1
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      }
    },
    getRows(rowIndex, classifyName) {
      let count = 0;
      for (let i = rowIndex; i < this.queryParams.fields.length; i++) {
        if (this.queryParams.fields[i].classifyName === classifyName) {
          count++;
        } else {
          break;
        }
      }
      return count;
    }
    //    spanMethod({row, column, rowIndex, columnIndex}) {
    //     this.queryParams.fields =  this.queryParams.fields.sort((a, b) => a.classifyName.localeCompare(b.classifyName))
    //     //定义需要合并的列字段，有哪些列需要合并，就自定义添加字段即可
    //   const fields = ['classifyName']
    //   // 当前行的数据
    //   const cellValue = row[column.property]

    //   // 判断只合并定义字段的列数据
    //   if (cellValue && fields.includes(column.property)) {
    //     const prevRow = this.queryParams.fields[rowIndex - 1] //上一行数据
    //     let nextRow = this.queryParams.fields[rowIndex + 1] //下一行数据
    //     // 当上一行的数据等于当前行数据时，当前行单元格隐藏
    //     if (prevRow && prevRow[column.property] === cellValue) {
    //       return { rowspan: 0, colspan: 0 }
    //     } else {
    //       // 反之，则循环判断若下一行数据等于当前行数据，则当前行开始进行合并单元格
    //       let countRowspan = 1 //用于合并计数多少单元格
    //       while (nextRow && nextRow[column.property] === cellValue) {
    //         nextRow = this.queryParams.fields[++countRowspan + rowIndex]
    //       }
    //       if (countRowspan > 1) {
    //         return { rowspan: countRowspan, colspan: 1 }
    //       }
    //     }
    //   }
    // }
  }
};
</script>
<style>
.hoverText{
  width: 100px;
  border: 1px solid red;
}
.el-table__body-wrapper .cell {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
 
.el-table__body-wrapper .cell:hover {
  overflow: visible;
  white-space: normal;
}
</style>
